using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System.ComponentModel;

namespace TVDesigner
{
    public class PropertyFileChooserEditor : UITypeEditor
    {
        private IWindowsFormsEditorService _wfes;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context == null)
            {
                return base.GetEditStyle(context);
            }
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(
            ITypeDescriptorContext context,
            IServiceProvider provider,
            object value)
        {
            if ((context == null) || (provider == null) || (context.PropertyDescriptor == null))
            {
                return base.EditValue(context, provider, value);
            }

            _wfes = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
            dialog.Multiselect = false;

            if (value != null)
            {
                dialog.FileName = value.ToString();
            }

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return (dialog.FileName);
            }
            else
            {
                return (value);
            }
        }

        //private DialogResult ShowForm(IServiceProvider provider, Form form)
        //{
        //    IUIService service = (IUIService)provider.GetService(typeof(IUIService));
        //    if (service != null)
        //    {
        //        return service.ShowDialog(form);
        //    }
        //    return form.ShowDialog();
        //}
    }
}
